package com.heiye.blog.oss.biz.factory;

import com.google.common.collect.Maps;
import com.heiye.blog.oss.biz.enums.FileStorageTypeEnum;
import com.heiye.blog.oss.biz.strategy.FileStrategy;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.Map;

/**
 * @author: heiye
 * @date: 2024/11/21 下午1:34
 * @version: v1.0.0
 * @description: TODO
 */
@Configuration
public class FileStrategyFactory {

    @Value("${storage.type}")
    private String strategyType;

    @Resource
    private List<FileStrategy> fileStrategyList;

    private final Map<FileStorageTypeEnum, FileStrategy> fileStrategyMap = Maps.newHashMap();

    /**
     * @PostConstruct： 构造方法执行完成之后执行将 textModelStrategyMap 填充
     */
    @PostConstruct
    public void init() {
        fileStrategyList.forEach(fileStrategy ->
                fileStrategyMap.put(fileStrategy.getStrategyTypeEnum(), fileStrategy));
    }

    public FileStrategy getStrategy() {
        FileStorageTypeEnum fileStorageTypeEnum = FileStorageTypeEnum.getStorageTypeEnum(strategyType);
        return fileStrategyMap.get(fileStorageTypeEnum);
    }
}
